package com.cloud.code.线程.关闭线程;

public class InterruptTest {

    //等待中的线程状态中断也可能抛出异常，需要在异常里面处理线程终止
    public static void main(String[] args) {

        Thread thread = new Thread(() -> {

            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
                System.out.println("我在执行。。。");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }, "my-thread");

        thread.start();

        System.out.println(thread.isInterrupted());
        thread.interrupt();
        System.out.println(thread.isInterrupted());
    }
}
